package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMStatMfdCriteriaMapper;
import cc.rengu.igas.momp.common.entity.TMStatMfdCriteria;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 手续费减免达标数据统计表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/20 11:05
 */

public class TMStatMfdCriteriaMapperImpl implements TMStatMfdCriteriaMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 使用商户编号和当前交易日期获取商户当前统计周期内已参加的商户手续费优惠营销活动的数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mchntNo   商户号
     * @param transDate 交易日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdCriteria 手续费减免达标数据统计表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/20 11:08
     */
    @Override
    public TMStatMfdCriteria selectTMStatMfdCriteriaByUsingMchntNoAndDateRange(String instId, String mchntNo, String transDate) throws Exception {

        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMchntNo(mchntNo);
        tmStatMfdCriteria.setPeriodStartDate(transDate);
        tmStatMfdCriteria.setPeriodEndDate(transDate);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMStatMfdCriteriaByUsingMchntNoAndDateRange");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdCriteria, tmStatMfdCriteria);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>商户号<{}>交易日期<{}>获取商户当前统计周期内已参与的商户手续费优惠营销活动统计数据失败!<{}>",
                    instId, mchntNo, transDate, iReturnCode);
            return null;
        }

        return tmStatMfdCriteria;
    }

    /**
     * 插入新增商户数据数据库操作接口实现
     *
     * @param tmStatMfdCriteriaList 手续费减免达标数据统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020-07-07 23:00
     */
    @Override
    public int insertNewMchntCriteriaData(List<TMStatMfdCriteria> tmStatMfdCriteriaList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertNewMchntCriteriaData");

        for (TMStatMfdCriteria tmStatMfdCriteria : tmStatMfdCriteriaList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>的新入驻商户<{}>的减免达标统计信息失败!<{}>", tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMcNo(), tmStatMfdCriteria.getMchntNo(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }

    /**
     * 更新手续费减免达标数据统计表数据
     *
     * @param tmStatMfdCriteria 手续费减免达标数据统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020-07-20 13:00
     */
    @Override
    public int updateTMstatMfdCriteriaByPrimaryKey(TMStatMfdCriteria tmStatMfdCriteria) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTMstatMfdCriteriaByPrimaryKey");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>商户<{}>的营销活动<{}>的规则<>的手续费减免达标数据统计表数据失败!<{}>",
                    tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo(), tmStatMfdCriteria.getMcNo(), tmStatMfdCriteria.getRecIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 使用商户编号，营销活动编号和交易日期获取商户达标数据统计 数据库操作接口定义
     *
     * @param instId    法人机构号
     * @param mchntNo   商户号
     * @param transDate 交易日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdCriteria 手续费减免达标数据统计表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/8/29 11:07
     */
    @Override
    public TMStatMfdCriteria selectTMStatMfdCriteriaByUsingMchntNoAndMcNoAndDateRange(String instId, String mchntNo, String transDate, String mcNo) throws Exception {

        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMchntNo(mchntNo);
        tmStatMfdCriteria.setPeriodStartDate(transDate);
        tmStatMfdCriteria.setPeriodEndDate(transDate);
        tmStatMfdCriteria.setMcNo(mcNo);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMStatMfdCriteriaByUsingMchntNoAndMcNoAndDateRange");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdCriteria, tmStatMfdCriteria);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>商户号<{}>交易日期<{}>获取商户当前统计周期内已参与的商户手续费优惠营销活动统计数据失败!<{}>",
                    instId, mchntNo, transDate, iReturnCode);
            return null;
        }

        return tmStatMfdCriteria;
    }

    /**
     * 更新手续费减免达标数据统计表统计结束日期数据
     *
     * @param tmStatMfdCriteria 手续费减免达标数据统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @since 2020-07-20 13:00
     */
    @Override
    public int updateTMstatMfdCriteriaEndDateByPrimaryKey(TMStatMfdCriteria tmStatMfdCriteria) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTMstatMfdCriteriaEndDateByPrimaryKey");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
            rglog.error("更新机构号<{}>商户<{}>的营销活动<{}>的规则<>的手续费减免达标数据统计表数据失败!<{}>",
                    tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo(), tmStatMfdCriteria.getMcNo(), tmStatMfdCriteria.getRecIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询指定商户、活动 统计开始日期大于指定日期数据
     *
     * @param instId  法人机构号
     * @param mchntNo 商户号
     * @param mcNo    活动号
     * @param txnDate 日期
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdCriteria>
     * @throws Exception 异常信息
     * @author JL PANG
     * @date 2020/9/19 18:58
     */
    @Override
    public List<TMStatMfdCriteria> selectCriteriaInfoListByinstIdMchntNoMcNoDate(String instId, String mchntNo, String mcNo, String txnDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);
        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMchntNo(mchntNo);
        tmStatMfdCriteria.setMcNo(mcNo);
        tmStatMfdCriteria.setPeriodStartDate(txnDate);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCriteriaInfoListByinstIdMchntNoMcNoDate");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取法人机构号<{}>的商户<{}>的营销活动<{}>手续费达标统计表信息列表失败!", tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo());
            return null;
        }

        return objectList.stream().map(item -> (TMStatMfdCriteria) (item)).collect(Collectors.toList());
    }

    /**
     * 删除手续费减免达标数据统计表统计结束日期数据
     *
     * @param tmStatMfdCriteriaList 手续费减免达标数据统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author JL Pang
     * @since 2020-9-19 13:00
     */
    @Override
    public int deletetmStatMfdCriteria(List<TMStatMfdCriteria> tmStatMfdCriteriaList) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "deletetmStatMfdCriteria");
        for (TMStatMfdCriteria tmStatMfdCriteria : tmStatMfdCriteriaList) {
            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
            if (Database.DBS_SUCCESS != iReturnCode && Database.DBS_NOT_FOUND != iReturnCode) {
                rglog.error("删除机构号<{}>商户<{}>的营销活动<{}>的规则<>的手续费减免达标数据统计表数据失败!<{}>",
                        tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo(), tmStatMfdCriteria.getMcNo(), tmStatMfdCriteria.getRecIndex(), iReturnCode);
                return iReturnCode;
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询指定活动 统计开始日期大于指定日期数据
     *
     * @param instId  法人机构号
     * @param mcNo    活动号
     * @param txnDate 日期
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdCriteria>
     * @throws Exception 异常信息
     * @author JL PANG
     * @date 2021/2/23 9:58
     */
    @Override
    public List<TMStatMfdCriteria> selectCriteriaInfoListByinstIdMcNoDate(String instId, String mcNo, String txnDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);
        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMcNo(mcNo);
        tmStatMfdCriteria.setPeriodStartDate(txnDate);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCriteriaInfoListByinstIdMcNoDate");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取法人机构号<{}>的商户<{}>的营销活动<{}>手续费达标统计表信息列表失败!", tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo());
            return null;
        }

        return objectList.stream().map(item -> (TMStatMfdCriteria) (item)).collect(Collectors.toList());
    }

    /**
     * 查询指定活动条数 统计开始日期大于指定日期数据
     *
     * @param instId  法人机构号
     * @param mcNo    活动号
     * @param txnDate 日期
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdCriteria>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/8/03 15:58
     */
    public TMStatMfdCriteria selectCriteriaInfoListCountByinstIdMcNoDate(String instId, String mcNo, String txnDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);
        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMcNo(mcNo);
        tmStatMfdCriteria.setPeriodStartDate(txnDate);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCriteriaInfoListCountByinstIdMcNoDate");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdCriteria, tmStatMfdCriteria);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("获取法人机构号<{}>的商户<{}>的营销活动<{}>手续费达标统计表信息列表条数失败!", tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo());
            return null;
        }
        return tmStatMfdCriteria;
    }

    /**
     * 更新指定机构号，活动手续费减免达标数据统计表优惠退出状态
     *
     * @param tmStatMfdCriteria 手续费减免达标数据统计表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author JL Pang
     * @since 2021-2-23 11:00
     */
    @Override
    public int updateTMstatMfdCriteriaExitDiscountStatusByPrimaryKey(TMStatMfdCriteria tmStatMfdCriteria) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateCriteriaInfoExitDiscountStatusByInstIdAndMcNo");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>商户<{}>的营销活动<{}>的规则<>的手续费减免达标数据统计表数据失败!<{}>",
                    tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo(), tmStatMfdCriteria.getMcNo(), tmStatMfdCriteria.getRecIndex(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询指定活动 当前日期内符合条件的达标统计表数据
     *
     * @param instId  法人机构号
     * @param mcNo    活动号
     * @param txnDate 日期
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdCriteria>
     * @throws Exception 异常信息
     * @author JL PANG
     * @date 2021/6/24 9:58
     */
    @Override
    public List<TMStatMfdCriteria> selectTMStatMfdCriteriaByMcNoAndDateRange(String instId, String mcNo, String txnDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.info("DBPoolName=[{}]", dbPoolName);
        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMcNo(mcNo);
        tmStatMfdCriteria.setPeriodStartDate(txnDate);
        tmStatMfdCriteria.setPeriodEndDate(txnDate);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMStatMfdCriteriaByMcNoAndDateRange");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取法人机构号<{}>的商户<{}>的营销活动<{}>手续费达标统计表信息列表失败!", tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo());
            return null;
        }

        return objectList.stream().map(item -> (TMStatMfdCriteria) (item)).collect(Collectors.toList());
    }

    /**
     * 查询指定活动条数 当前日期内符合条件的达标统计表数据
     *
     * @param instId  法人机构号
     * @param mcNo    活动号
     * @param txnDate 日期
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdCriteria>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/8/03 15:58
     */
    public TMStatMfdCriteria selectTMStatMfdCriteriaCountByMcNoAndDateRange(String instId, String mcNo, String txnDate) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);
        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMcNo(mcNo);
        tmStatMfdCriteria.setPeriodStartDate(txnDate);
        tmStatMfdCriteria.setPeriodEndDate(txnDate);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMStatMfdCriteriaCountByMcNoAndDateRange");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdCriteria, tmStatMfdCriteria);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("获取法人机构号<{}>的商户<{}>的营销活动<{}>手续费达标统计表信息列表条数失败!", tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo());
            return null;
        }
        return tmStatMfdCriteria;
    }

    /**
     * 查询商户当前周期优惠额度上限
     *
     * @param tmStatMfdCriteria 达标统计报表实体
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMStatMfdCriteria>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/6/23 9:58
     */
    @Override
    public List<Object> selectMchntQuotaInfoList(TMStatMfdCriteria tmStatMfdCriteria) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntQuotaInfoList");
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件获取商户当前周期手续费优惠额度上限值失败!");
            return null;
        }

        return objectList.stream().map(item -> (TMStatMfdCriteria) (item)).collect(Collectors.toList());

    }

    /**
     * 查询商户当前周期优惠额度上限 分页查询
     *
     * @param tmStatMfdCriteria 达标统计报表实体
     * @param pageInfo          分页
     * @return cc.rengu.oltp.utility.bean.PageResult 查询结果列表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/6/23 9:58
     */
    @Override
    public PageResult selectMchntQuotaInfoPageList(TMStatMfdCriteria tmStatMfdCriteria, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntQuotaInfoList");

        return dbsUtil.dbsFetchPageListData(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class, pageInfo);
    }

    /**
     * 修改商户当前周期优惠额度上限
     *
     * @param tmStatMfdCriteria 达标统计报表实体
     * @return int 返回码
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/6/23 9:58
     */
    @Override
    public int updateMchntFeeTxnAmtLimit(TMStatMfdCriteria tmStatMfdCriteria) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr;
        if (StringUtil.isNullorEmpty(tmStatMfdCriteria.getMrctDisUplmtEffDt())) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntQuotaInfo");
        } else {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntQuotaInfoAndMrctDisUplmtEffDt");
        }

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("更新机构号<{}>商户<{}>的当前周期优惠额度上限失败 <{}>",
                    tmStatMfdCriteria.getInstId(), tmStatMfdCriteria.getMchntNo(), iReturnCode);
            return iReturnCode;
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 分页查询所传机构及下属机构所有商户达标统计表数据
     *
     * @param tmStatMfdCriteria 商户列表查询使用实体类入参
     * @param pageInfo          数据库操作分页实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 查询结果列表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/8/11 13:18
     */
    @Override
    public PageResult selectMchntByorgCode(TMStatMfdCriteria tmStatMfdCriteria, PageInfo pageInfo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntFeeSurplusDiscountList");

        return dbsUtil.dbsFetchPageListData(sqlStr, tmStatMfdCriteria, TMStatMfdCriteria.class, pageInfo);
    }

    /**
     * 使用商户编号和当前交易日期获取商户当前统计周期内已参加的商户手续费优惠营销活动的数据库操作接口实现
     *
     * @param instId    法人机构号
     * @param mchntNo   商户号
     * @param transDate 交易日期
     * @return cc.rengu.igas.momp.common.entity.TMStatMfdCriteria 手续费减免达标数据统计表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/8/25 11:08
     */
    @Override
    public TMStatMfdCriteria selectTMStatMfdCriteriaByMchntNoAndDateRange(String instId, String mchntNo, String transDate) throws Exception {

        TMStatMfdCriteria tmStatMfdCriteria = new TMStatMfdCriteria();
        tmStatMfdCriteria.setInstId(instId);
        tmStatMfdCriteria.setMchntNo(mchntNo);
        tmStatMfdCriteria.setPeriodStartDate(transDate);
        tmStatMfdCriteria.setPeriodEndDate(transDate);

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMStatMfdCriteriaByMchntNoAndDateRange");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatMfdCriteria, tmStatMfdCriteria);
        if (Database.DBS_SUCCESS != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}>商户号<{}>交易日期<{}>获取商户当前统计周期内已参与的商户手续费优惠营销活动统计数据失败!<{}>",
                    instId, mchntNo, transDate, iReturnCode);
            return null;
        }

        return tmStatMfdCriteria;
    }
}
